import { Elysia } from 'elysia'
import { cors } from '@elysiajs/cors'

const app = new Elysia()
  .use(cors())
  .get('/', ({ body }) => {
    return Bun.file('./static/index.html')
  })
  .post('/api/watermark/android_qr/up_load/take_picture', async ({ body }) => {
    console.log('body', body.image)
    // 1 二维码不存在 2角度倾斜  3清晰度不够  4完整图片
    // 保存文件到uploads目录
    if (body.image) {
      const filePath = `./uploads/${Date.now()}_${body.type}_${body.size}_${body.value || ''}.png`
      try {
        // 将文件保存到本地
        await Bun.write(filePath, body.image)
        console.log(`文件已保存到: ${filePath}`)
      } catch (error) {
        console.log('err', error)
      }
    }

    return {
      code: 1001
    }
  })
  .listen(8000)

console.log(`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`)
